import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:untitled1/app/res/myColors.dart';
import 'package:untitled1/app/widgets/TitleBar.dart';

class LoginPapgeController extends GetxController{
  var name=''.obs;
  var pwd=''.obs;
  var phone_number=''.obs;
  var is_select=false.obs;

  final int totalSeconds; // 倒计时总秒数
  late int _initialSeconds;
  Timer? _timer;

  var remaining = 0.obs; // 剩余时间：可监听

  LoginPapgeController({this.totalSeconds = 60}) {
    _initialSeconds = totalSeconds;
  }

  /// 是否在倒计时中
  bool get isCounting => remaining.value > 0;

  /// 开始倒计时
  void startCountdown() {
    if (isCounting) return; // 防止重复启动

    remaining.value = _initialSeconds;

    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (remaining.value == 1) {
        timer.cancel();
      }
      remaining.value--;
    });
  }

  /// 重置倒计时
  void resetCountdown() {
    _timer?.cancel();
    remaining.value = 0;
  }

  @override
  void onClose() {
    _timer?.cancel(); // 控制器销毁时释放定时器
    super.onClose();
  }

}